package com.dw.etl.core.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dw.etl.core.bean.vo.LayMsg;
import com.dw.etl.core.bean.vo.Msg;
import com.dw.etl.core.bean.model.SysRoles;
import com.dw.etl.core.bean.model.SysTag;
import com.dw.etl.core.bean.model.SysTagPermissions;
import com.dw.etl.core.bean.model.SysUsersRoles;
import com.dw.etl.core.mapper.SysTagMapper;
import com.dw.etl.core.mapper.SysTagPermissionsMapper;
import com.dw.etl.core.api.TagService;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author GuoPeng
 * @Title: TagServiceImpl
 * @Description: TODO
 * @date 2019/5/14 17:53
 */
@Service
@Transactional
public class TagServiceImpl implements TagService {


    @Resource
    private SysTagMapper sysTagMapper;


    @Resource
    private SysTagPermissionsMapper sysTagPermissionsMapper;

    @Override
    public LayMsg getTagList(Integer page, Integer limit) {
         PageHelper.startPage(page,limit );
         Page<SysTag> sysTags=   (Page)sysTagMapper.selectAll();
        return LayMsg.success().addCount(sysTags.getTotal()).addData(sysTags.getResult());
    }


    @Override
    public Msg updateTag(SysTag sysTag) {
        int i = sysTagMapper.updateByPrimaryKeySelective(sysTag);
        if(i>0){
            return  Msg.success();
        }else {
            return  Msg.fail();
        }
    }

    @Override
    public Msg delectTag(Integer id) {
        int i = sysTagMapper.deleteByPrimaryKey(id);
        if(i>0){
            return  Msg.success();
        }
        return Msg.fail();
    }


    @Override
    public Msg insertTag(SysTag sysTag) {
        int i = sysTagMapper.insert(sysTag);
        if(i>0){
            return  Msg.success();
        }else {
            return Msg.fail();
        }
    }


    @Override
    public Msg PermissionsSetTag(Map map, Integer id) {

        /*全部移除*/
        HashMap parm = new HashMap();
        parm.put("permissions_id", id);
        List<SysTagPermissions> sysTagPermissions = sysTagPermissionsMapper.selectbypridAndtagId(parm);
        for(int i=0;i<sysTagPermissions.size();i++ ){
            sysTagPermissionsMapper.deleteByPrimaryKey(sysTagPermissions.get(i).getId());
        }


        Iterator it = map.entrySet().iterator();
        /*添加角色*/
        while (it.hasNext()){
            Map.Entry entry = (Map.Entry)it.next();
            HashMap parm0 = new HashMap();
            parm0.put("permissions_id", id);
            parm0.put("tag_id", Integer.parseInt(entry.getKey().toString()));
            List<SysTagPermissions>  sysTagPermissions1 = sysTagPermissionsMapper.selectbypridAndtagId(parm0);
            if(sysTagPermissions1.size()<1){
                SysTagPermissions sysTagPermissions2 = new SysTagPermissions();
                sysTagPermissions2.setTagId(Integer.parseInt(entry.getKey().toString()));
                sysTagPermissions2.setPermissionsId(id);
                int i = sysTagPermissionsMapper.insert(sysTagPermissions2);

            }
        }

        return Msg.success();
    }

    @Override
    public Msg SelectAll(Integer id ) {
        List<SysTag> selectall = sysTagMapper.selectAll();

        List<SysTagPermissions> sysTagPermissions=null;
        ArrayList list = new ArrayList();
        HashMap parm = new HashMap();
        parm.put("permissions_id", id);
        for (int n=0;n<selectall.size();n++) {
            sysTagPermissions=  sysTagPermissionsMapper.selectbypridAndtagId(parm);
            for(int m=0;m< sysTagPermissions.size();m++){
                if(sysTagPermissions.get(m).getTagId()==selectall.get(n).getId()){
                    HashMap data = new HashMap();
                    data.put("name", selectall.get(n).getDescription());
                    data.put("id", selectall.get(n).getId());
                    data.put("star",true );
                    list.add(data);
                    selectall.remove(n);
                }
            }

        }

        for (int c=0;c<selectall.size();c++) {
            HashMap data = new HashMap();
            data.put("name", selectall.get(c).getDescription());
            data.put("id", selectall.get(c).getId());
            data.put("star",false );
            list.add(data);
        }
        return  Msg.success().add("info", list);
    }
}
